﻿using mozhi.smarterp.AuthorCenter.OpenIddict.Repositories;
using mozhi.smarterp.AuthorCenter.Permissions.Repositories;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Clients;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
using Volo.Abp.PermissionManagement;
using Volo.Abp.TenantManagement;

namespace mozhi.smarterp.AuthorCenter.Permissions
{
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(CusomtPermissionDataSeedContributor), typeof(PermissionDataSeedContributor), typeof(IDataSeedContributor))]
    public class CusomtPermissionDataSeedContributor : PermissionDataSeedContributor
    {
        private IPermissionGroupRepository _permissionGroupRepository;
        private IPermissionGroupDataSeeder _permissionGroupDataSeeder;
        private IPermissionGrantRepository _permissionGrantRepository;
        private IGuidGenerator _guidGenerator;

        public CusomtPermissionDataSeedContributor(IPermissionDefinitionManager permissionDefinitionManager, IPermissionDataSeeder permissionDataSeeder, ICurrentTenant currentTenant , 
            IPermissionGroupRepository permissionGroupRepository, IPermissionGroupDataSeeder permissionGroupDataSeeder ,IPermissionGrantRepository permissionGrantRepository , IGuidGenerator guidGenerator) :
            base(permissionDefinitionManager, permissionDataSeeder, currentTenant)
        {
            _permissionGroupRepository = permissionGroupRepository;
            _permissionGroupDataSeeder = permissionGroupDataSeeder;
            _permissionGrantRepository= permissionGrantRepository;
            _guidGenerator = guidGenerator;
        }


        public override async Task SeedAsync(DataSeedContext context)
        {
            if (CurrentTenant.Id.HasValue)
                return;

            var multiTenancySide = CurrentTenant.GetMultiTenancySide();

            await _permissionGroupDataSeeder.SeedAsync();

            var permissionGroups = await _permissionGroupRepository.GetListAsync(null, 100000, 0, null, true, new List<Enums.PermissionType>() { Enums.PermissionType.PlatformItem });

            var permissionGrants = await _permissionGrantRepository.GetListAsync(RolePermissionValueProvider.ProviderName, "admin");

            if (permissionGrants.Count > 0)
                return;

            var permissions = new List<PermissionItem>();

            permissionGroups.ForEach(group =>
            {
                permissions.AddRange(group.Items);
            });

            var permissionNames = permissions.Select(x => x.Code).ToList();

            if (permissionNames.Count == 0)
                return;

            string[] names = permissionNames.ToArray();
            List<string> second = (await _permissionGrantRepository.GetListAsync(names, RolePermissionValueProvider.ProviderName, "admin")).Select((PermissionGrant x) => x.Name).ToList();
            foreach (string item in names.Except(second))
            {
                await _permissionGrantRepository.InsertAsync(new PermissionGrant(_guidGenerator.Create(), item, RolePermissionValueProvider.ProviderName, "admin", null),true);
            }
        }

        #region

        #endregion
    }
}
